package com.yonyou.pmclouds.constructionlog.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.annotation.Insert;
import com.yonyou.pmclouds.basecom.entity.AdditionalPropertiesVOWrapper;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.service.CommonPropertiesProcessor;
import com.yonyou.pmclouds.constructionlog.entity.ConstructionLogListViewVO;
import com.yonyou.pmclouds.constructionlog.entity.ConstructionLogVO;
import com.yonyou.pmclouds.constructionlog.rmiitf.ConstructionLogMaintain;
import com.yonyou.pmclouds.constructionlog.rmiitf.ConstructionLogQuery;
import com.yonyou.pmclouds.enterprise.entity.EnterpriseInfo;
import com.yonyou.pmclouds.inspectitem.entity.InspectItemVO;
import com.yonyou.pmclouds.inspectitem.rmiitf.InspectItemService;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Slf4j
@Controller
@RequestMapping(value = "/mobile/constructionlog")
public class ConstructionLogController {
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ConstructionLogMaintain maintain;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ConstructionLogQuery query;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private InspectItemService inspectItemService;
    @Autowired
    private UserInfoQuery userQuery;
    @Autowired
    private EnterpriseInfoQuery constructionunitQuery;
    @Autowired
    private CommonPropertiesProcessor propertiesProcessor;

    private static final String[] USER_ID_FIELDS = {ConstructionLogVO.CREATOR};
    private static final String[] USER_NAME_FIELDS = {ConstructionLogVO.CREATOR_NAME};


    @Insert
    @RequestMapping(value = "/add")
    @ResponseBody
    @PMApprove
    public ConstructionLogVO add(@RequestBody ConstructionLogVO vo) throws BusinessException {
        ConstructionLogVO svo = maintain.add(vo);
        userQuery.tranUserInfoField(new ConstructionLogVO[]{svo}, USER_ID_FIELDS, USER_NAME_FIELDS);
        return vo;
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    @PMApprove(trigger = "commit")
    public ConstructionLogVO update(@RequestBody ConstructionLogVO vo) throws BusinessException {
        ConstructionLogVO svo = maintain.update(vo);
        userQuery.tranUserInfoField(new ConstructionLogVO[]{svo}, USER_ID_FIELDS, USER_NAME_FIELDS);
        return vo;
    }
    
    @RequestMapping(value = "/queryByProject")
    @ResponseBody
    public AdditionalPropertiesVOWrapper[] queryByProject(String pk_project) throws BusinessException {
        ConstructionLogListViewVO[] vos = query.queryByProject(pk_project);
        if (vos == null || vos.length == 0) {
            return null;
        }
        userQuery.tranUserInfoField(vos, USER_ID_FIELDS, USER_NAME_FIELDS);
        return propertiesProcessor.wrapAdditionalProps(vos);
    }
    
    @RequestMapping(value = "/getDetail")
    @ResponseBody
    public ConstructionLogVO queryByPk(String pk_constructionlog) throws BusinessException {
        ConstructionLogVO constructionLog = query.queryOne(pk_constructionlog);
        userQuery.tranUserInfoField(new ConstructionLogVO[]{constructionLog}, USER_ID_FIELDS, USER_NAME_FIELDS);
        // 施工单位
        String pk_constructionunit = constructionLog.getPk_constructionunit();
        if(!StringUtils.isEmpty(pk_constructionunit)) {
            EnterpriseInfo enterpriseInfo = constructionunitQuery.queryEnterpriseById(pk_constructionunit);
            constructionLog.setConstruction_name(enterpriseInfo.getName());
        }
        return constructionLog;
        
    }
    
    @RequestMapping(value = "/getInspectItemDetail")
    @ResponseBody
    public InspectItemVO queryInspectItemVO(String pk_inspectitem) throws BusinessException {
        return inspectItemService.queryOne(pk_inspectitem);
    }

    public void queryRef() throws BusinessException {
        boolean flag = query.queryRefByIds(ConstructionLogVO.PK_PROJECT, new String[]{"5494da8e-d62c-4815-8a26-65ccf75ab17c"});
        if(flag) {
            System.out.println(flag);
        }
    }
//    @RequestMapping(value = "/getPics")
//    @ResponseBody
//    public String[] getPics(HttpServletResponse response,String pk_siteinspection) throws BusinessException {
//        response.setHeader("Access-Control-Allow-Origin", "*");
//        return query.queryPics(pk_siteinspection);
//    }
}
